package sg.edu.nus.iss.cerberus.views;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import sg.edu.nus.iss.cerberus.handler.CreateTestMethodActionExecutor;
import sg.edu.nus.iss.cerberus.util.CerberusConstants;
import sg.edu.nus.iss.cerberus.util.CerberusMethodsDelegate;

/*
 * Author : Priyanka and Lin
 */
public class CerberusIObjectActionDelegate implements IObjectActionDelegate {

	// Addition of the compilation unit to call the pop up wizard
	private ICompilationUnit compilationUnit;
	// Checking the ISelection type
	private ISelection selection;

	public CerberusIObjectActionDelegate() {

	}

	@Override
	public void run(IAction action) {
		Object structuredSelection = ((IStructuredSelection) selection)
				.getFirstElement();

		if (structuredSelection instanceof IPackageFragmentRoot) {
			CerberusMethodsDelegate.createPopupDialogue(
					CerberusConstants.WINDOW_TITLE,
					CerberusConstants.UNIMPL_MSG);
		}

		else if (structuredSelection instanceof IPackageFragment) {
			CerberusMethodsDelegate.createPopupDialogue(
					CerberusConstants.WINDOW_TITLE,
					CerberusConstants.UNIMPL_MSG);
		}

		else if (structuredSelection instanceof ICompilationUnit) {
			compilationUnit = (ICompilationUnit) structuredSelection;
			CreateTestMethodActionExecutor.getInstance()
					.executeCreateTestMethodAction(compilationUnit);
		} else {

			CerberusMethodsDelegate.createPopupDialogue(
					CerberusConstants.WINDOW_TITLE,
					CerberusConstants.UNIMPL_MSG);

		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		this.selection = selection;
	}

	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// TODO Auto-generated method stub
	}

}